home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
source
/
music4c.sit
/
Music4C Folder
/
SFConvert folder
/
SFConvertInterface.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-25
|
13KB
|
615 lines
/*
* Converts soundfiles between various formats:
* "interleaved", "AIFF", Sound Designer, and floating point.
* ⌐ Graeme Gerrard October 1988
*/
#include "SFConvert.h"
#include <stdio.h>
#include <unix.h>
#include <string.h>
#include <math.h>
#include <SANE.h>
#include "SDtype.h"
#define WNE_TRAP_NUM 0x60
#define UNIMPL_TRAP_NUM 0x9F
#define SUSPEND_RESUME_BIT 0x0001
#define SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define RESUMING 1
#define ACTIVATING 1
int IFileOpen;
int OFileOpen;
long TotalSamps;
CursHandle watchCurs;
int SoundFileType;
long SampleRate;
long NumChannels;
double MaxSample;
double MinSample;
long SoundFileDirID;
long StartUpDirID;
int SFSaveVRef;
long fileSize;
int nrec;
Str255 SoundFileName;
Str255 StartDirName, SFDirectoryName;
Str255 NewSoundFileName;
ioParam myIOParmBlk;
ioParam NewParmBlk;
int SFOUTPUTtype;
Boolean SDnoResource;
FILE *ReportFile;
OSErr theErr;
Boolean gWNEImplemented, gDone;
EventRecord event;
long RecLength;
SysEnvRec SysEnvData;
void init_process(void);
void CheckSystem(void);
extern Boolean OpenSFRead(void);
Boolean GetOutputPref(void);
extern Boolean OpenSFWrite(void);
Boolean ConvertFile(void);
void GetMusic4C_Prefs(void);
extern Boolean updateNewSFResFile(void);
extern void DoOSErrorAlert(Str255, Str255);
extern void OSError(Str255, Str255);
extern Boolean CloseSF(void);
void MainLoop(void);
ProcPtr restartProc(void);
void init_mgrs(void);
void init(void);
void fill_menus(void);
void identify(void);
void fill_menus(void);
void HandleEvent(void);
void do_mouse_down(EventRecord *);
void do_menu(long);
void Finish(void);
extern void OutLineButton(DialogPtr, int);
void FixUp(void);
extern Boolean FloatToINT16(void);
extern Boolean FloatToCHUNKY(void);
extern Boolean FloatToSD1(void);
extern Boolean FloatToSD2(void);
extern Boolean FloatToAIFF(void);
extern Boolean AIFFToSD(void);
extern Boolean AIFFToINT16(void);
extern Boolean AIFFToFloat(void);
extern Boolean InterleavedToSD1(void);
extern Boolean InterleavedToSD2(void);
extern Boolean InterleavedToCHUNKY(void);
extern Boolean InterleavedToAIFF(void);
extern Boolean InterleavedToFloat(void);
extern Boolean SD1ToAIFF(void);
extern Boolean SD2ToAIFF(void);
extern Boolean SD2ToFloat(void);
extern Boolean SD1ToFloat(void);
extern Boolean SD2ToFloat(void);
extern Boolean SD2ToCHUNKY(void);
extern Boolean SD1ToCHUNKY(void);
extern Boolean SD2ToINT16(void);
extern Boolean SD1ToINT16(void);
extern Boolean SetSFDir(Str255, long *);
main()
{
SetApplLimit(GetApplLimit());
MaxApplZone();
MoreMasters();
init_process();
CheckSystem();
if ( OpenSFRead() ) {
IFileOpen = TRUE;
if ( GetOutputPref() ) {
if ( OpenSFWrite() ) {
OFileOpen = TRUE;
HiliteMenu(0);
if ( ConvertFile() )
if ( !updateNewSFResFile() ) {
DoOSErrorAlert("\pError updating resource fork for new sound file", NIL);
}
}
}
if ( IFileOpen || OFileOpen) {
CloseSF();
IFileOpen = FALSE;
OFileOpen = FALSE;
}
}
MainLoop();
ExitToShell();
} /* main */
ProcPtr restartProc()
{
/*ExitToShell();*/
}
void init_process()
{
init_mgrs();
init();
fill_menus();
}
void init_mgrs()
{
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent, 0 );
InitWindows();
InitCursor();
InitMenus();
TEInit();
InitDialogs(restartProc);
}
void init()
{
GetMusic4C_Prefs();
IFileOpen = FALSE;
OFileOpen = FALSE;
SampleRate = 44100L;
watchCurs = GetCursor(watchCursor);
RecLength = (long)(16384);
}
void identify()
{
DialogPtr aboutBox;
EventRecord event;
PicHandle thePic;
Rect myRect, tempRect;
aboutBox = GetNewDialog(ABOUT_DIALOG, NIL, -1L);
tempRect.top = aboutBox->portRect.top;
tempRect.left = aboutBox->portRect.left;
tempRect.bottom = aboutBox->portRect.bottom;
tempRect.right = aboutBox->portRect.right;
tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;
tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;
MoveWindow(aboutBox, tempRect.left, tempRect.top, TRUE);
ParamText("\p", NIL, NIL, NIL);
ShowWindow(aboutBox);
DrawDialog(aboutBox);
SelectWindow(aboutBox);
SetPort(aboutBox);
while( !EventAvail(keyDownMask|autoKeyMask|mDownMask, &event))
SystemTask();
if ( event.what == mouseDown ) {
GlobalToLocal(&event.where);
if ( PtInRect(event.where, &aboutBox->portRect))
GetNextEvent(mDownMask, &event);
}
DisposDialog(aboutBox);
}
void fill_menus()
{
Str255 mess;
if (GetResource('MENU', APPLE_MENU)==0) {
SysBeep(20);
DoOSErrorAlert("\pcan't open resource file", NIL);
return;
}
AppendMenu(GetMenu(APPLE_MENU), CtoPstr("About SFConvert..."));
AppendMenu(GetMenu(APPLE_MENU), CtoPstr("(-"));
AddResMenu(GetMenu(APPLE_MENU), 'DRVR');
InsertMenu(GetMenu(APPLE_MENU), 0);
InsertMenu(GetMenu(FILE_MENU), 0);
DisableItem(GetMHandle(FILE_MENU), 2);
DrawMenuBar();
}
void MainLoop()
{
gDone = FALSE;
gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
NGetTrapAddress ( UNIMPL_TRAP_NUM, ToolTrap ) );
while( gDone == FALSE ) {
HandleEvent();
}
}
void HandleEvent()
{
char c;
Boolean valid;
WindowPtr update_window;
/* if ( gWNEImplemented )
WaitNextEvent(everyEvent, &event, SLEEP, NIL_MOUSE_REGION );
else {
*/
SystemTask();
valid = GetNextEvent(everyEvent, &event );
if ( !valid) return;
switch(event.what) {
case nullEvent:
break;
case mouseDown:
do_mouse_down(&event);
break;
case mouseUp:
break;
case keyDown:
case autoKey:
c = event.message & charCodeMask;
if(event.modifiers & cmdKey) {
if(event.what != autoKey )
do_menu(MenuKey(c));
}
break;
case keyUp:
break;
case updateEvt:
break;
case diskEvt:
break;
case activateEvt:
break;
case networkEvt:
break;
case driverEvt:
break;
case app1Evt:
break;
case app2Evt:
break;
case app3Evt:
break;
case app4Evt:
if ( ( event.message & SUSPEND_RESUME_BIT ) == RESUMING )
/* resume event */;
else
/* suspend event */
;
break;
default:
break;
}
}
void do_mouse_down(event)
EventRecord *event;
{
WindowPtr windowp;
int windowCode = FindWindow(MK_LONG(event->where), &windowp);
switch(windowCode) {
case inDesk:
break;
case inMenuBar:
do_menu(MenuSelect(event->where));
break;
case inSysWindow:
SystemClick(event, windowp);
break;
case inContent:
break;
case inDrag:
break;
case inGrow:
break;
case inGoAway:
break;
default:
break;
}
}
void do_menu(command)
long command;
{
int menu_id = HiWord(command);
int item = LoWord(command);
char item_name[32];
switch(menu_id) {
case APPLE_MENU:
if ( item == AboutItem)
identify();
else {
GetItem(GetMHandle(menu_id), item, item_name);
OpenDeskAcc(item_name);
}
break;
case FILE_MENU:
switch(item) {
case OPEN:
if ( OpenSFRead() ) {
IFileOpen = TRUE;
if ( GetOutputPref() ) {
if ( OpenSFWrite() ) {
OFileOpen = TRUE;
HiliteMenu(0);
if ( ConvertFile() )
if ( !updateNewSFResFile() ) {
DoOSErrorAlert("\pError updating resource fork for new sound file", NIL);
}
}
}
if ( IFileOpen || OFileOpen) {
CloseSF();
IFileOpen = FALSE;
OFileOpen = FALSE;
}
}
break;
case QUIT:
Finish();
break;
}
break;
}
HiliteMenu(0);
}
Boolean ConvertFile()
{
SetCursor(*GetCursor(watchCursor));
switch ( SoundFileType ) {
case FLOAT:
if (SFOUTPUTtype == INT16)
if ( FloatToINT16() )
return(TRUE);
if (SFOUTPUTtype == AIFF)
if ( FloatToAIFF() )
return(TRUE);
if (SFOUTPUTtype == SD1)
if ( FloatToSD1())
return(TRUE);
if (SFOUTPUTtype == SD2)
if ( FloatToSD2())
return(TRUE);
break;
case SD1:
if (SFOUTPUTtype == INT16)
if ( SD1ToINT16())
return(TRUE);
if (SFOUTPUTtype == AIFF)
if ( SD1ToAIFF() )
return(TRUE);
if (SFOUTPUTtype == FLOAT)
if ( SD1ToFloat() )
return(TRUE);
break;
case SD2:
if (SFOUTPUTtype == INT16)
if ( SD2ToINT16())
return(TRUE);
if (SFOUTPUTtype == AIFF)
if ( SD2ToAIFF() )
return(TRUE);
if (SFOUTPUTtype == FLOAT)
if ( SD2ToFloat() )
return(TRUE);
break;
case INT16:
if (SFOUTPUTtype == SD1)
if ( InterleavedToSD1() )
return(TRUE);
if (SFOUTPUTtype == AIFF)
if ( InterleavedToAIFF() )
return(TRUE);
if (SFOUTPUTtype == FLOAT)
if ( InterleavedToFloat() )
return(TRUE);
break;
case AIFF:
if (SFOUTPUTtype == SD1)
if ( AIFFToSD() )
return(TRUE);
if (SFOUTPUTtype == INT16)
if ( AIFFToINT16() )
return(TRUE);
if (SFOUTPUTtype == FLOAT)
if ( AIFFToFloat() )
return(TRUE);
break;
default:
InitCursor();
DoOSErrorAlert("\pAIFF,float,16bit or Sound Designer formats only", NIL);
return(FALSE);
break;
}
InitCursor();
return(FALSE);
}
void Finish()
{
ExitToShell();
}
Boolean GetOutputPref()
{
int itemHit;
int itype;
Rect box, tempRect;
ControlHandle radHand1, radHand2, radHand3, radHand4;
DialogPtr myDialog;
myDialog = GetNewDialog(OUTPUT_FORMAT, FALSE, (WindowPtr) -1);
tempRect.top = myDialog->portRect.top;
tempRect.left = myDialog->portRect.left;
tempRect.bottom = myDialog->portRect.bottom;
tempRect.right = myDialog->portRect.right;
tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;
tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;
MoveWindow(myDialog, tempRect.left, tempRect.top, TRUE);
if ( !myDialog ) {
return;
}
/* get handles to the radio buttons for run type */
GetDItem(myDialog, ILEAVEDitem, &itype, &radHand1, &box);
GetDItem(myDialog, AIFFitem, &itype, &radHand2, &box);
GetDItem(myDialog, SDitem, &itype, &radHand3, &box);
GetDItem(myDialog, FLTitem, &itype, &radHand4, &box);
switch(SoundFileType) {
case INT16:
HiliteControl(radHand1, 255); /* INT16 */
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 1);
HiliteControl(radHand2, 255); /* AIFF */
HiliteControl(radHand3, 255); /* SD */
SFOUTPUTtype = FLOAT;
break;
case AIFF:
HiliteControl(radHand2, 255); /* AIFF */
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 1);
HiliteControl(radHand1, 255); /* INT16 */
HiliteControl(radHand3, 255); /* SD */
SFOUTPUTtype = FLOAT;
break;
case SD1:
case SD2:
HiliteControl(radHand1, 255); /* INT16 */
HiliteControl(radHand3, 255); /* SD */
HiliteControl(radHand2, 255); /* AIFF */
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 1);
SFOUTPUTtype = FLOAT;
break;
case FLOAT:
HiliteControl(radHand4, 255); /* FLOAT */
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 1);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 0);
SFOUTPUTtype = AIFF;
break;
}
ShowWindow(myDialog);
OutLineButton(myDialog, USERItem);
SelectWindow(myDialog);
SetPort(myDialog);
itemHit = 0;
while ( itemHit != OKButton && itemHit != CancelButton) {
ModalDialog((ProcPtr)NIL, &itemHit);
switch (itemHit) {
case ILEAVEDitem:
SFOUTPUTtype = INT16;
SetCtlValue(radHand1, 1);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 0);
break;
case AIFFitem:
SFOUTPUTtype = AIFF;
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 1);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 0);
break;
case SDitem:
SFOUTPUTtype = SD1;
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 1);
SetCtlValue(radHand4, 0);
break;
case FLTitem:
SFOUTPUTtype = FLOAT;
SetCtlValue(radHand1, 0);
SetCtlValue(radHand2, 0);
SetCtlValue(radHand3, 0);
SetCtlValue(radHand4, 1);
break;
}
}
DisposDialog(myDialog);
if ( itemHit == OKButton )
return(TRUE);
else
return(FALSE);
}
void FixUp()
{
if ( StartDirName[0] != NIL ) {
SetSFDir(StartDirName, &StartUpDirID);
}
}
void CheckSystem()
{
theErr = SysEnvirons(1, &SysEnvData);
if (SysEnvData.hasFPU)
return;
OSError("\pThis version of SFConvert",
"\prequires a floating point unit");
ExitToShell();
}